#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2021/2/27 14:56
# @Author  : cjw
import logging
import os

from setting.setting import BASE_PATH

LOG_NAME = 'caseLogger'
LOG_FILE = os.path.join(BASE_PATH, 'logs', 'case.log')
LOG_LEVEL = logging.INFO
LOG_FORMATTER = ('%(asctime)s [%(name)s] [%(filename)s] [%(levelname)s] : %(message)s',
                 None)


def create_logger(log_name: str = LOG_NAME,
                  log_file: str = LOG_FILE,
                  log_level: int = LOG_LEVEL,
                  log_formatter: tuple = LOG_FORMATTER):
	"""
	:param log_name: 日志名称
	:param log_file: 日志文件位置
	:param log_level: 日志等级
	:param log_formatter: 日志格式
	:return:
	"""
	formatter = logging.Formatter(*log_formatter)
	# 日志处理器
	file_handler = logging.FileHandler(log_file, encoding='utf-8')
	file_handler.setFormatter(formatter)

	stream_handler = logging.StreamHandler()
	stream_handler.setFormatter(formatter)

	# 日志记录器
	logger = logging.getLogger(log_name)
	logger.setLevel(log_level)

	# 关联日志记录器和处理器
	logger.addHandler(file_handler)
	logger.addHandler(stream_handler)

	return logger


log = create_logger()

if __name__ == '__main__':
	import os

	# print(os.path.join(BASE_PATH, 'demo.log'))
	log.info('这是一个误解函数', extra={'status': 'PASS'})
